<template>
  <div class="big-box">
    <!-- 登录页 -->
    <div class="form-box">
      <h3>欢迎登录~~</h3>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
      >
        <el-form-item prop="userName">
          <el-input
            :prefix-icon="User"
            placeholder="用户名"
            v-model="ruleForm.userName"
          />
        </el-form-item>
        <el-form-item prop="passWord">
          <el-input
            placeholder="密码"
            :show-password="true"
            :prefix-icon="Lock"
            v-model="ruleForm.passWord"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            @click="submitForm(ruleFormRef)"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import useUserStore from '@/store/modules/user'
import useMenuStore from '@/store/modules/menu'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getMenu } from '@/api/menu'
import type { menuResponse } from '@/api/menu/types'
import { SET_MENU, GET_MENU } from '@/utils/menu'

let $router = useRouter()
let userStore = useUserStore()
let menuStore = useMenuStore()
let loading = ref(false)
// import {loginApi} from '@/api/user'
const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
const rules = reactive<FormRules<RuleForm>>({
  userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  passWord: [{ required: true, message: '请输入密码', trigger: 'blur' }],
})
interface RuleForm {
  userName: string
  passWord: string
}
let ruleForm = reactive<RuleForm>({
  userName: '',
  passWord: '',
})
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid) => {
    loading.value = true
    if (valid) {
      try {
        await userStore.login(ruleForm)
        ElMessage.success('登录成功')
        getMenus().then(() => {
          console.log('chenggong')
        })
        loading.value = false
      } catch (error) {
        console.log(error)
        loading.value = false
      }
    }
  })
}
const getMenus = async () => {
  const res: menuResponse = await getMenu({
    userId: localStorage.getItem('userId') || '',
  })
  if (res.code === 200) {
    console.log(res)
    // 存储token
    const data = res.data.menuList
    if (data) $router.push('/')
    SET_MENU(data)
    // 存储用户信息
    return 'OK'
  } else {
    return Promise.reject(new Error(res.msg))
  }
}
</script>

<style lang="scss" scoped>
.big-box {
  width: 100vw;
  height: 100vh;
  background-color: #2459ec;
  display: flex;
  justify-content: center;
  align-items: center;
  h3 {
    font-size: 22px;
    font-weight: 550;
    margin-bottom: 20px;
  }
  .form-box {
    width: 500px;
    height: 300px;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .el-button {
      width: 100%;
    }
    .el-form {
      width: 80%;
    }
  }
}
</style>
